package com.ruoyi.college.controller;

import com.ruoyi.college.domain.AdminClassTrainingLink;
import com.ruoyi.college.service.IAdminClassTrainingLinkService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.trainingManagement.domain.trainingProgram;
import com.ruoyi.trainingManagement.service.ItrainingProgramService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 班级培养方案中间表Controller
 * 
 * @author chenxl
 * @date 2025-05-19
 */
@RestController
@RequestMapping("/college/classTrainingLink")
public class AdminClassTrainingLinkController extends BaseController
{
    @Autowired
    private IAdminClassTrainingLinkService adminClassTrainingLinkService;
    @Autowired
    private ItrainingProgramService trainingProgramService;



    /**
     * 查询班级培养方案中间表列表
     */
    //@PreAuthorize("@ss.hasPermi('college:classTrainingLink:list')")
    @GetMapping("/list")
    public TableDataInfo list(AdminClassTrainingLink adminClassTrainingLink)
    {
        startPage();
        List<AdminClassTrainingLink> list = adminClassTrainingLinkService.selectAdminClassTrainingLinkList(adminClassTrainingLink);
        return getDataTable(list);
    }
    /**
     * 根据行政班ID获取关联的培养方案列表
     */
    @GetMapping("/getByClassId/{classId}")
    public AjaxResult getTrainingProgramsByClassId(@PathVariable String classId) {
        try {
            //打印接收到的参数
            System.out.println("接收到的参数：" + classId);
            // 1. 查询中间表获取培养方案ID列表
            List<String> programIds = adminClassTrainingLinkService.listTrainingProgramIdsByClassId(classId);
            if (programIds.isEmpty()) {
                return AjaxResult.success("无关联培养方案");
            }

            // 2. 批量查询培养方案详情
            List<trainingProgram> programs = trainingProgramService.listByIds(programIds);
            return AjaxResult.success(programs);
        } catch (Exception e) {
            return AjaxResult.error("查询失败：" + e.getMessage());
        }
    }


    /**
     * 导出班级培养方案中间表列表
     */
    @PreAuthorize("@ss.hasPermi('college:classTrainingLink:export')")
    @Log(title = "班级培养方案中间表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AdminClassTrainingLink adminClassTrainingLink)
    {
        List<AdminClassTrainingLink> list = adminClassTrainingLinkService.selectAdminClassTrainingLinkList(adminClassTrainingLink);
        ExcelUtil<AdminClassTrainingLink> util = new ExcelUtil<AdminClassTrainingLink>(AdminClassTrainingLink.class);
        util.exportExcel(response, list, "班级培养方案中间表数据");
    }

    /**
     * 获取班级培养方案中间表详细信息
     */
    @PreAuthorize("@ss.hasPermi('college:classTrainingLink:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(adminClassTrainingLinkService.selectAdminClassTrainingLinkById(id));
    }

    /**
     * 批量新增班级培养方案中间表
     */
    @PreAuthorize("@ss.hasPermi('college:classTrainingLink:add')")
    @Log(title = "班级培养方案中间表", businessType = BusinessType.INSERT)
    @PostMapping("/batch")
    public AjaxResult batchAdd(@RequestBody List<AdminClassTrainingLink> adminClassTrainingLink) {
        try {
            //打印接收到的参数
            System.out.println("接收到的参数：" + adminClassTrainingLink);
            // 调用服务层批量插入方法
            int rows = adminClassTrainingLinkService.batchInsertAdminClassTrainingLink(adminClassTrainingLink);
            return AjaxResult.success("批量新增成功，共插入 " + rows + " 条记录");
        } catch (Exception e) {
            return AjaxResult.error("批量新增失败: " + e.getMessage());
        }
    }

    /**
     * 修改班级培养方案中间表
     */
    @PreAuthorize("@ss.hasPermi('college:classTrainingLink:edit')")
    @Log(title = "班级培养方案中间表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AdminClassTrainingLink adminClassTrainingLink)
    {
        return toAjax(adminClassTrainingLinkService.updateAdminClassTrainingLink(adminClassTrainingLink));
    }

    /**
     * 删除班级培养方案中间表
     */
    @PreAuthorize("@ss.hasPermi('college:classTrainingLink:remove')")
    @Log(title = "班级培养方案中间表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(adminClassTrainingLinkService.deleteAdminClassTrainingLinkByIds(ids));
    }

}
